/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.util;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.easoftware.code.beans.ComboItem;
import net.easoftware.code.beans.Mes;
import net.easoftware.code.beans.facade.SelfPopulate;
import net.easoftware.code.error.EaError;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.DateConverter;

/**
 *
 * @author eliu
 */
public class Tools {

    static {
        ConvertUtils.register(new DateConverter(null), Date.class);
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
    }

    /**
     *
     * @return
     */
    public static List<ComboItem> getTiposTarget() {
        List<ComboItem> val = new LinkedList<>();
        val.add(new ComboItem("1", "DSO"));
        val.add(new ComboItem("2", "OVERDUE"));

        return val;
    }

    /**
     *
     * @return
     */
    public static List<ComboItem> getEstados() {
        List<ComboItem> val = new LinkedList<>();
        val.add(new ComboItem("1", "Activo"));
        val.add(new ComboItem("0", "Inactivo"));

        return val;
    }

    /**
     *
     * @return
     */
    public static List<ComboItem> getTemplatesSeguridad() {
        List<ComboItem> val = new LinkedList<>();
        val.add(new ComboItem("1", "Administrador"));
        val.add(new ComboItem("2", "Consultas/Cargas"));
        val.add(new ComboItem("3", "Consultas"));
        return val;
    }

    /**
     *
     * @param destType
     * @param src
     * @return
     * @throws EaError
     */
    public static List cloneList(Class destType, List src) throws EaError {
        List destList = new LinkedList();
        for (Object c : src) {
            destList.add(cloneObject(c, destType));
        }
        return destList;
    }

    /**
     *
     * @param src
     * @param dest
     * @return
     * @throws EaError
     */
    public static Object cloneObject(Object src, Class dest) throws EaError {
        try {
            Object d = dest.newInstance();
            if (src == null) {
                return d;
            }
            BeanUtils.copyProperties(d, src);
            if (d instanceof SelfPopulate) {
                ( (SelfPopulate) d ).populate(src);
            }
            return d;
        }
        catch (InvocationTargetException | InstantiationException | IllegalAccessException ex) {
            throw new EaError("Error clonando objetos", ex);
        }
    }

    /**
     *
     * @param src
     * @param dest
     * @throws EaError
     */
    public static void cloneObject(Object src, Object dest) throws EaError {
        try {
            BeanUtils.copyProperties(src, dest);
        }
        catch (InvocationTargetException | IllegalAccessException ex) {
            throw new EaError("Error clonando objetos", ex);
        }
    }
    private static String[] arregloDeMeses = {"Enero", "Febrero",
        "Marzo", "Abril", "Mayo", "Junio", "Julio",
        "Agosto", "Septiembre", "Octubre", "Noviembre",
        "Diciembre"};

    public static List<Mes> getMeses() {
        List<Mes> meses = new LinkedList<>();
        for (int i = 0; i < arregloDeMeses.length; i++) {
            meses.add(new Mes(i, arregloDeMeses[i]));
        }
        return meses;
    }

    public static Map<Integer, Mes> getMesesMap() {
        Map<Integer, Mes> map = new LinkedHashMap<>();
        for (Mes m : getMeses()) {
            map.put(m.getNumeroMes(), m);
        }

        return map;
    }
}
